package com.study.spwar.web.fileupload;

import com.study.spwar.utils.SysUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

@Slf4j
@Controller
@RequestMapping("file")
public class FileUpController {
    //	获取配置文件中的值
    @Resource
    Environment environment;

    /**
     * * 在不知道文件名情况下获取上传文件
     */
    @RequestMapping(value = "upfile", method = RequestMethod.POST)
    public @ResponseBody
    String uploadOssFile(MultipartFile file, HttpServletRequest request, Person person) {
        String picPath = environment.getProperty("pic.Path");
        String picUrl = environment.getProperty("pic.Url");
        log.info(StringUtils.repeat('-', 28));
        String realPath = request.getServletContext().getRealPath("");
        System.out.println("realPath="+realPath);
        log.info("pserson:{}|picPath:{}", person.toString(),picPath);
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = mr.getFileMap();
        Set<Map.Entry<String, MultipartFile>> entries = fileMap.entrySet();
        String url = null;
        for (Map.Entry<String, MultipartFile> entry : entries) {
            MultipartFile multipartFile = entry.getValue();
            String key = entry.getKey();
            url = multipartFile.getOriginalFilename();
            log.info("key-value|表单字段和文件名分别是|:{},{}", key, multipartFile.getOriginalFilename());
            try {
                // TODO 此方法无法自动创建不存在的文件夹
                // multipartFile.transferTo(new File(picPath +
                // multipartFile.getOriginalFilename()));

                // 文件保存
                url = SysUtils.getDateStrSpan_() + "/" + SysUtils.getDate4() + multipartFile.getOriginalFilename();
                // TODO 可以自动创建不存在的文件夹
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(picPath + url));
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return String.format("%s/%s", picUrl, url);
    }
}

class Person {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Person() {
        super();
    }

    /**
     * @param name
     * @param age
     */
    public Person(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}
